package com.dd.controller;


import com.dd.pojo.Commodity;
import com.dd.pojo.Course;
import com.dd.service.CourseService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    CourseService courseService;

    @RequestMapping("/selectk")
    public String selectk() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<Course> list = courseService.selectk();
        String str = mapper.writeValueAsString(list);
        String jso ="{\"code\":0,\"msg\":\"\",\"count\":" + 25 + ",\"data\":" + str + "}";
        return jso;
    }

    @RequestMapping("/addk")
    public String addk(Course course){
        courseService.addk(course);
        return "1";
    }

    @RequestMapping("/updatek")
    public String updatek(Course course){
        courseService.updatek(course);
        return "1";
    }

    @RequestMapping("/deletek")
    public String deletek(int id){
        courseService.deletek(id);
        return "1";
    }

    @RequestMapping("/selectbykname")
    public String selectbykname(String kname) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<Course> selectbykname = courseService.selectbykname(kname);
        String str = mapper.writeValueAsString(selectbykname);
        String k ="{\"code\":0,\"msg\":\"\",\"count\":" + 25 + ",\"data\":" + str + "}";
        return k;
    }
}
